(function () {
  var that = window.commonFunc
  var Toast = window.toast
  var vm = new Vue({
    el: "#qa-index",
    data: {
      errorDialog: false,
      code: '',
      luckText: null,
      prizeId: null,
      prizeShortName : null,
      Id: null,
      end: false,
      clickNum: 0,
      showIntro: false,
      start: false
    },
    created: function () {
      var _this = this
      _this.showIntro = localStorage.getItem('showIntro') || false
      if (!_this.showIntro) {
        setTimeout(function(){
          introJs().setOptions({
            prevLabel: "上一步",
            nextLabel: "下一步",
            skipLabel: "跳过",
            doneLabel: "结束",
            showStepNumbers: false,
            tooltipClass: 'hide'
          }).start()
        },200)
      }
      var locationUrl = location.href
      if (that.getQueryString('isinfo') === null) {
        window.location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx4bf091c2f1ce91db&redirect_uri=https://wx.tesoon.com/api/envelope/getuserinfo.html&response_type=code&scope=snsapi_userinfo&state=' + locationUrl + '#wechat_redirect'
      } else {
        this.getUserInfo()
      }
      $('.btn').on('click', function () {
        _this.showIntro = localStorage.getItem('showIntro') || false
        if(!_this.showIntro) {
          localStorage.setItem('showIntro', true)
          introJs().exit()
        }else {
          if(!_this.start) {
            that.modal.alert({
              closeImg: './assets/images/close.jpg',
              text: '活动暂未开始', //提示文字内容
              btnOk: '关闭', //确定按钮文字内容
              ok: function () {},
              close: function () {}
            })
            return
          }
          if(_this.clickNum <= 0) {
            _this.luckText = '今日抽奖次数已用完</br>明天再来吧'
            that.modal.alert({
              closeImg: './assets/images/close.jpg',
              text: _this.luckText, //提示文字内容
              btnOk: '关闭', //确定按钮文字内容
              ok: function () {},
              close: function () {}
            })
          }else {
            _this.initHB()
          }
        }
      })
    },
    mounted: function () {
      that.pixel2rem()
    },
    methods: {
      getUserInfo: function () {
        var _this = this
        $.ajax({
          url: api.userInfo,
          success: function (res) {
            _this.clickNum = res.clicknum
            _this.start = !!parseInt(res.isupdate)
            $('.times span').text(res.clicknum)
          }
        })
      },
      initHB: function () {
        var _this = this
        // _this.clickNum = _this.clickNum - 1
        _this.clickNum = _this.clickNum <= 0 ? 0 : _this.clickNum - 1
        $('.times span').text(_this.clickNum)
        // 请求奖品
        _this.getLuck()
        var win = (parseInt($(".hongbao").css("width"))) - 60;
        $('.hongbao').show()
        // $(".couten").css("height", $(document).height());
        $(".backward").show()
        $('.index-body').css('position', 'fixed')
        $("li").css({});
        var del = function () {
          nums++;
          //					console.info(nums);
          //					console.log($(".li" + nums).css("left"));
          $(".li" + nums).remove();
          setTimeout(del, 200)
        }

        var numz = 4;
        //倒数计时
        var backward = setInterval(function () {
          numz--
          if (numz > 0) {
            $(".backward span").html(numz)
          } else {
            $(".backward span").html('3')
            $(".backward").hide()
            clearInterval(backward)
            // 增加红包开始
            var num = 0
            var addluck = setInterval(function () {
              var hb = parseInt(Math.random() * (3 - 1) + 1);
              var Wh = parseInt(Math.random() * (70 - 40) + 40);
              var Left = parseInt(Math.random() * (win - 0) + 0);
              var rot = (parseInt(Math.random() * (45 - (-45)) - 45)) + "deg";
              //				console.log(rot)
              num++;
              $(".couten").append("<li class='li" + num + "' ><a href='javascript:;'><img src='assets/images/hb_" + hb + ".png'></a></li>");
              $(".li" + num).css({
                "left": Left,
              })

              $(".li" + num + " a img").css({
                "width": Wh,
                "transform": "rotate(" + rot + ")",
                // "-webkit-transform": "rotate(" + rot + ")",
                "-ms-transform": "rotate(" + rot + ")", /* Internet Explorer */
                "-moz-transform": "rotate(" + rot + ")", /* Firefox */
                "-webkit-transform": "rotate(" + rot + ")", /* Safari 和 Chrome */
                "-o-transform": "rotate(" + rot + ")" /* Opera */
              })

              $(".li" + num).animate({'top': $(window).height() + 20}, 4000, function () {
                //删掉已经显示的红包
                this.remove()
              })

              //点击红包抽奖
              $('.couten').on('click', ".li" + num, function () {
                $(this).find('img').attr('src', './assets/images/bz1.png').animate({width:"90px"}, 100, function () {
                  $(this).remove()
                })
                // this.remove()
              //   _this.getLuck()
              //   // $(".mo").css("display", "block")
              })
            }, 500)
            //倒计时开始
            var time = 8
            var daojishi = setInterval( function () {
              time = time - 1
              // 时间到
              if (time <= 3) {
                clearInterval(addluck)
              }
              if (time <= 0) {
                time = 8
                $('.hongbao').hide()
                $('.index-body').css('position', 'relative')
                clearInterval(daojishi)
                console.log(_this.prizeShortName)
                if (_this.prizeShortName != '4') {
                  that.modal.alert({
                    closeImg: './assets/images/close.jpg',
                    isIcon: true,
                    iconSrc: _this.luckImg, //图片链接地址
                    text: _this.luckText, //提示文字内容
                    tip: '稍后可在“我的奖品”中查询获奖记录。</br>提醒：一定要填写个人邮寄信息哦！',
                    btnOk: '前去领奖', //确定按钮文字内容
                    ok: function () {
                      window.location.href = './pages/receive-information.html?prizeid=' + _this.prizeId + '&prizeshortname=' + _this.prizeShortName + '&id=' + _this.Id
                    },
                    close: function () {}
                  })
                } else {
                  that.modal.alert({
                    closeImg: './assets/images/close.jpg',
                    text: _this.luckText, //提示文字内容
                    btnOk: '关闭', //确定按钮文字内容
                    ok: function () {},
                    close: function () {}
                  })
                }
              }
              $('.timer').text(time)
            }, 1000)
          }
        }, 1000)
      },
      getLuck: function () {
        var _this = this
        $.ajax({
          url: api.getLuck,
          success: function(res) {
            if (res.error == '1') {
              _this.prizeShortName = '4'
              _this.luckText = '今日抽奖次数已用完</br>明天再来吧'
            } else {
              _this.prizeId = res.prizeid
              _this.prizeShortName = res.prizeshortname
              _this.Id = res.id
              switch (res.prizeshortname) {
                case '1':
                  _this.luckText = '恭喜获得一等奖'
                  _this.luckImg = './assets/images/newgift1.jpg'
                  break;
                case '2':
                  _this.luckText = '恭喜获得二等奖'
                  _this.luckImg = './assets/images/newgift2.jpg'
                  break;
                case '3':
                  _this.luckText = '恭喜获得三等奖'
                  _this.luckImg = './assets/images/newgift4.jpg'
                  break;
                case '4':
                  _this.luckText = '没抽中，再接再厉！'
                  break;
                default:
                  break;
              }
            }
          }
        })
      }
    }
  })
})()
